/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package openrule;

import com.centerkey.utils.BareBonesBrowserLaunch;
import java.awt.Cursor;
import java.util.Iterator;
import java.util.Vector;
import openrule.event.RulerEvent;
import openrule.event.RulerListener;

/**
 * This is the base class for the Horizontal and Vertical Ruler mode. This class 
 * encapsulates the common functions used by the two classes.
 * 
 * @author pborrego
 */
public class BaseRuler extends javax.swing.JFrame {
    protected Vector<RulerListener> rlListeners;
    
    public BaseRuler()
    {
        super();
        rlListeners = new Vector<openrule.event.RulerListener>();
    }
    
    public void addRulerListener(RulerListener rl)
    {
        rlListeners.add(rl);
    }

    public void fireRulerEvent(RulerEvent evt)
    {
        Iterator<RulerListener> i = rlListeners.iterator();
        RulerListener l;

        while(i.hasNext())
        {
            l = i.next();

            switch(evt.getEventType())
            {
                case RulerListener.CLOSE:
                    l.close(evt);
                    break;
                case RulerListener.COMMAND:
                    l.command(evt);
                    break;
                case RulerListener.FLIP:
                    l.flip(evt);
                    break;
                case RulerListener.MINIMIZE:
                    l.minimize(evt);
                    break;
                case RulerListener.NUDGE:
                    l.nudge(evt);
                    break;
            }
        }
    }
    
    protected void gotohelp() {
        BareBonesBrowserLaunch.openURL("http://code.google.com/p/leadiv-projects/w/list");
    }
    
    /* Firing RulerEvents */
    protected void about() {
        fireRulerEvent(new RulerEvent(RulerListener.COMMAND, this, "a"));
    }
    
    protected void snap() {
        fireRulerEvent(new RulerEvent(RulerListener.COMMAND, this, "s"));
    }

    protected void colorRuler() {
        fireRulerEvent(new RulerEvent(RulerListener.COMMAND, this, "c"));
    }

    protected void moveRuler(java.awt.event.ActionEvent evt) {
        fireRulerEvent(new RulerEvent(RulerListener.NUDGE, this, evt.getActionCommand()));
    }
    
    protected void minimizeRuler() {
        fireRulerEvent(new RulerEvent(RulerListener.MINIMIZE, this));
    }           

    protected void flipRuler() {
        fireRulerEvent(new RulerEvent(RulerListener.FLIP, this));
    }
    
    protected void closeRuler() {
        fireRulerEvent(new RulerEvent(RulerListener.CLOSE, this));
    }
    
    /* Cursor Functions */
    protected void resizeHeightCursor() {
        this.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
    }

    protected void resizeWidthCursor() {
        this.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
    }
    
    protected void defaultCursor() {
        this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }

    protected void moveCursor() {
        this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
    }
}
